import {
    BlockComponentTypes,
    EnchantmentTypes,
    ItemComponentTypes,
    ItemStack,
} from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";
import { Game, GameComponent } from "@sapi-game/main";
import { CvSMainState } from "../mainState";
import { RandomUtils } from "@sapi-game/utils";
import { Timer } from "@sapi-game/gameComponent/common/timer/timer";

interface ChestItem {
    item?: ItemStack;
    gravity: number;
    buildItem: () => ItemStack;
}

export class CvSChestManager extends GameComponent<CvSMainState> {
    itemPool: ChestItem[] = [
        {
            gravity: 5,
            buildItem() {
                const pick = new ItemStack("iron_pickaxe");
                const dur = pick.getComponent(ItemComponentTypes.Durability)!;
                dur.damage = dur.maxDurability;
                pick.setCanDestroy(["iron_bars"]);
                return pick;
            },
        },
        {
            gravity: 5,
            buildItem() {
                const pick = new ItemStack("stone_pickaxe");
                const dur = pick.getComponent(ItemComponentTypes.Durability)!;
                dur.damage = dur.maxDurability;
                pick.setCanDestroy(["iron_bars"]);
                return pick;
            },
        },
        {
            gravity: 1,
            buildItem() {
                const pick = new ItemStack("diamond_pickaxe");
                //加附魔
                const enchant = pick.getComponent(
                    ItemComponentTypes.Enchantable
                )!;
                enchant.addEnchantment({
                    type: EnchantmentTypes.get("efficiency")!,
                    level: 5,
                });
                //改耐久
                const dur = pick.getComponent(ItemComponentTypes.Durability)!;
                dur.damage = dur.maxDurability;
                pick.nameTag = "§b§l神镐";
                pick.setCanDestroy(["iron_bars"]);
                return pick;
            },
        },
    ];

    override onAttach(): void {
        //构建所有物品
        this.itemPool.forEach((e) => {
            e.item = e.buildItem();
        });
        this.refresh();
        //定时刷新
        const timer = this.state.getComponent(Timer);
        this.subscribe(
            timer.events.onTime,
            () => {
                this.refresh();
                timer.set(this.context.config.chestRefreshInterval);
            },
            { time: 0 }
        );
    }

    refresh() {
        for (let pos of this.context.map.chestPoints) {
            try {
                const inv = Dimensions.Overworld.getBlock(pos)?.getComponent(
                    BlockComponentTypes.Inventory
                );
                if (!inv?.container) continue;
                inv.container.clearAll();
                const item = RandomUtils.weightedChoice(
                    this.itemPool,
                    this.itemPool.map((i) => i.gravity)
                );
                if (!item?.item) return;
                inv.container.addItem(item.item);
            } catch (err) {}
        }
        this.context.chicken.sendMessage("§a箱子刷新了");
    }
}
